package com.miguo.view;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.miguo.framework.view.CustomGridView;
import com.miguo.ui.R;

import java.util.Calendar;

/**
 * one month view.
 *
 * @author Sazonov-adm
 */
public class CalendarMonthView extends LinearLayout {

    /**
     * date with which view was init.
     */
    private Calendar mInitialMonth;
    /**
     * adapter
     */
    private CalendarAdapter mDaysAdapter;

    /**
     * context.
     */
    private Context mContext;
    /**
     * Конструктор.
     *
     * @param context
     * контекст
     */
    private CustomGridView gridview;

    public CalendarMonthView(final Context context) {
        this(context, null);
    }

    /**
     * Конструктор.
     *
     * @param context контекст
     * @param attrs   атрибуты
     */
    public CalendarMonthView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.calendar, this, true);

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        mInitialMonth = Calendar.getInstance();
        if (null == mDaysAdapter) {
            mDaysAdapter = new CalendarAdapter(mContext, mInitialMonth);
        }
        gridview = (CustomGridView) findViewById(R.id.calendar_days_gridview);
        if (Build.VERSION.SDK_INT >= 9) {
            gridview.setOverScrollMode(View.OVER_SCROLL_NEVER);
        }
        gridview.setAdapter(mDaysAdapter);
        initDayCaptions(mContext);
    }

    /**
     * @param context context.
     */
    private void initDayCaptions(final Context context) {
        String[] dayCaptions = new String[]{"日", "一", "二", "三", "四", "五", "六"};
        GridView captionsGridView = (GridView) findViewById(R.id.calendar_captions_gridview);
        if (Build.VERSION.SDK_INT >= 9) {
            captionsGridView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        }
        captionsGridView.setAdapter(new ArrayAdapter<String>(context,
                R.layout.calendar_caption_item, R.id.calendar_caption_date,
                dayCaptions));
    }

    public final void updateItem(int date) {
        gridview.getChildAt(mDaysAdapter.getIndext() + date - 1)
                .findViewById(R.id.date)
                .setBackgroundResource(R.drawable.shape_hollow_circle_red_bg);
    }
}
